//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI.WebControls;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Files.Components;

namespace CommunityServer.Files.Controls
{
	/// <summary>
	/// Summary description for BlogRatePost.
	/// </summary>
	public class EntryRatePost : CommunityServer.Controls.RatingControl
	{
		CSContext csContext = CSContext.Current;

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			ApplicationKey = csContext.ApplicationKey;
			PostID = csContext.PostID;
		}

		[DefaultValue( null )]
		public virtual string ApplicationKey
		{
			get
			{
				Object state = ViewState["ApplicationKey"];
				if(state != null)
					return (string)state;
				return null;
			}
			set { ViewState["ApplicationKey"] = value; }
		}

		[DefaultValue( -1 )]
		public virtual int PostID
		{
			get
			{
				Object state = ViewState["PostID"];
				if(state != null)
					return (int)state;
				return -1;
			}
			set { ViewState["PostID"] = value; }
		}

		private Entry entry;
		public Entry Post
		{
			get
			{
				if(entry == null)
					entry = Entries.GetEntry(ApplicationKey, PostID);
				return entry;
			}
			set { entry = value; }
		}

		private double _currentRating = -1;
		public override double CurrentRating
		{
			get
			{
				if (_currentRating == -1)
					_currentRating = this.Post.RatingAverage;

				return _currentRating;
			}
			set
			{
				_currentRating = value;
			}
		}

		private int _currentVotes = -1;
		public override int CurrentVotes
		{
			get
			{
				if (_currentVotes == -1)
					_currentVotes = this.Post.TotalRatings;

				return _currentVotes;
			}
			set
			{
				_currentVotes = value;
			}
		}

		private string _viewDetailsScript = null;
		public override string ViewDetailsScript
		{
			get
			{
				if (_viewDetailsScript == null)
					_viewDetailsScript = "OpenWindow('" + Globals.GetSiteUrls().PostRating(Post.ThreadID) + "')";

				return _viewDetailsScript;
			}
			set
			{
				_viewDetailsScript = value;
			}
		}

		public override bool UserCanRate 
		{ 
			get
			{
				if (csContext.User.IsAnonymous)
					return false;

				FileGalleryPermission permission = Folders.GetFolder(ApplicationKey).ResolvePermission( csContext.User ) as FileGalleryPermission;
				if(!permission.Vote)
					return false;

				return this.Post.EnableRatings;
			}
		}

		public override void Rate(double value)
		{
			int currentRating = Ratings.GetRating(RatingType.Thread, this.Post.ThreadID, csContext.User.UserID);

			Rating rating = new Rating();
			rating.User = csContext.User;
			rating.Value = (int) value;
			rating.RatingType = RatingType.Thread;
			rating.ItemID = this.Post.ThreadID;
			Ratings.Rate(rating);

			Entries.ClearEntriesCache(-1, -1, this.Post.PostID);

			if (currentRating == -1)
			{
				_currentVotes = this.Post.TotalRatings + 1;
				_currentRating = (this.Post.RatingSum + value) / (this.Post.TotalRatings + 1);
			}
			else
				_currentRating = ((this.Post.RatingSum - currentRating) + value) / this.Post.TotalRatings;
		}


	}
}
